package org.csu.jpetstore.util;

import org.csu.jpetstore.service.CartService;
import org.csu.jpetstore.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * https://blog.csdn.net/weixin_43097056/article/details/118850291
 */
@Component
public class MySpringBeanCreateUtil {

    private static MySpringBeanCreateUtil mySpringBeanCreateUtil;
    @Resource
    private ItemService itemService;
    @Autowired
    private CartService cartService;

    public static ItemService getItemService() {
        return mySpringBeanCreateUtil.itemService;
    }

    public static CartService getService2() {
        return mySpringBeanCreateUtil.cartService;
    }

    //在springboot依赖加载后运行且只运行一次
    @PostConstruct
    public void init() {
        mySpringBeanCreateUtil = this;
        mySpringBeanCreateUtil.itemService = this.itemService;
        mySpringBeanCreateUtil.cartService = this.cartService;
    }
}